package com.demo.controller;

import com.demo.common.CreateDate;
import com.demo.common.CreateUUID;
import com.demo.common.Pyindo;
import com.demo.common.ShaEncrypt;
import com.demo.model.TabUsers;
import com.demo.service.TabUsersService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@Log4j2
public class TabUsersController {

    @Resource
    private TabUsersService service;

    @RequestMapping(value = "tab_users_view")
    public String tab_users_view(String loginname,HttpServletRequest request){
        if(null==loginname){
            loginname = "";
        }
        TabUsers t = new TabUsers();
        t.setLoginname(loginname);
        service.findAll(t,request);
        request.setAttribute("loginname",loginname);
        return "index/tab_users/view";
    }


    @RequestMapping(value = "tab_users_pre_save")
    public String tab_users_pre_save(){
        return "index/tab_users/save";
    }

    @RequestMapping(value = "tab_users_save")
    public String tab_users_save(TabUsers t,HttpServletRequest request){
        List<TabUsers> check_loginname_list = service.findByloginname(t.getLoginname());
        if(check_loginname_list.isEmpty()){
            String id = CreateUUID.getuuid();
            String source = "后台";
            String loginname = (String)request.getSession().getAttribute("loginname");
            String datetimes = CreateDate.getDate();
            t.setPassword(ShaEncrypt.shaEncode(t.getPassword()));
            t.setId(id);
            t.setEm1(source);
            t.setEm2(loginname);
            t.setEm3(datetimes);
            if(t.getSex().equals("男"))
            {
                t.setEm4("m.jpg");
            }
            if(t.getSex().equals("女"))
            {
                t.setEm4("w.jpg");
            }
            t.setEm5("");
            boolean b = service.save(t);
            request.setAttribute("b",b);
        }else{
            request.setAttribute("b",false);
        }
        return "index/tab_users/save";
    }

    @RequestMapping(value = "do_username_ajax")
    public void  do_username_ajax(String username, HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        String pinyin_username = Pyindo.getPingYin(username);
        out.print(pinyin_username);

    }

    @RequestMapping(value = "tab_users_update_flag_open")
    public String tab_users_update_flag_open(TabUsers users,HttpServletRequest request){
        users.setFlag("0");
        service.update_flag(users);
        return "redirect:tab_users_view";
    }

    @RequestMapping(value = "tab_users_update_flag_close")
    public String tab_users_update_flag_close(TabUsers users,HttpServletRequest request){
        users.setFlag("1");
        service.update_flag(users);
        return "redirect:tab_users_view";
    }

    @RequestMapping(value = "tab_users_pre_update")
    public String tab_users_pre_update(String id,HttpServletRequest request){
        TabUsers t = service.findByid(id);
        request.setAttribute("t",t);
        return "index/tab_users/update";
    }

    @RequestMapping(value = "tab_users_update")
    public String tab_users_update(TabUsers t,HttpServletRequest request){
        String loginname = (String)request.getSession().getAttribute("loginname");
        String datetimes = CreateDate.getDate();
        t.setEm2(loginname);
        t.setEm3(datetimes);
        t.setEm4("");
        t.setEm5("");
        boolean b = service.update(t);
        request.setAttribute("b",b);
        return "index/tab_users/update";
    }

    @RequestMapping(value = "tab_users_del")
    public String tab_users_del(String id){
        service.del(id);
        return "redirect:tab_users_view";
    }

}
